package com.ldzl.controller.inventory;

import com.ldzl.pojo.CkStockTaking;
import com.ldzl.pojo.CkStockTakingResult;
import com.ldzl.service.CkStockTakingService;
import com.ruoyi.common.core.domain.UR;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存盘点控制器
 */
@RestController
@RequestMapping("/inventory/stocktaking")
public class StockTakingController extends BaseController {

    @Autowired
    private CkStockTakingService ckStockTakingService;

    /**
     * 查询库存盘点列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CkStockTaking ckStockTaking) {
        startPage();
        List<CkStockTaking> list = ckStockTakingService.selectCkStockTakingList(ckStockTaking);
        return getDataTable(list);
    }

    /**
     * 获取库存盘点详细信息
     */
    @GetMapping("/{takingId}")
    public UR getInfo(@PathVariable("takingId") Long takingId) {
        Map<String, Object> data = new HashMap<>();
        data.put("data", ckStockTakingService.selectCkStockTakingById(takingId));
        return UR.ok("成功", data);
    }

    /**
     * 新增库存盘点
     */
    @PostMapping
    public UR add(@RequestBody CkStockTaking ckStockTaking) {
        return toUR(ckStockTakingService.insertCkStockTaking(ckStockTaking));
    }

    /**
     * 修改库存盘点
     */
    @PutMapping
    public UR edit(@RequestBody CkStockTaking ckStockTaking) {
        return toUR(ckStockTakingService.updateCkStockTaking(ckStockTaking));
    }

    /**
     * 删除库存盘点
     */
    @DeleteMapping("/{takingIds}")
    public UR remove(@PathVariable Long[] takingIds) {
        return toUR(ckStockTakingService.deleteCkStockTakingByIds(takingIds));
    }
    
    /**
     * 查询盘点明细列表
     */
    @GetMapping("/details/list")
    public TableDataInfo detailsList(CkStockTakingResult ckStockTakingResult) {
        startPage();
        List<CkStockTakingResult> list = ckStockTakingService.selectCkStockTakingResultList(ckStockTakingResult.getTakingId());
        return getDataTable(list);
    }
    
    /**
     * 提交盘点单
     */
    @PutMapping("/submit/{takingId}")
    public UR submit(@PathVariable("takingId") Long takingId) {
        return toUR(ckStockTakingService.submitCkStockTaking(takingId));
    }
    
    /**
     * 审核盘点单
     */
    @PutMapping("/approve")
    public UR approve(@RequestBody CkStockTaking ckStockTaking) {
        return toUR(ckStockTakingService.approveCkStockTaking(ckStockTaking));
    }
    
    /**
     * 取消盘点单
     */
    @PutMapping("/cancel/{takingId}")
    public UR cancel(@PathVariable("takingId") Long takingId) {
        return toUR(ckStockTakingService.cancelCkStockTaking(takingId));
    }
    
    /**
     * 查询入库记录
     */
    @GetMapping("/inbound/{takingId}")
    public UR inboundRecords(@PathVariable("takingId") Long takingId) {
        // 模拟返回入库记录数据
        List<Object> records = new ArrayList<>(generateMockInboundRecords(takingId));
        return UR.ok("入库记录查询成功", records);
    }
    
    /**
     * 查询出库记录
     */
    @GetMapping("/outbound/{takingId}")
    public UR outboundRecords(@PathVariable("takingId") Long takingId) {
        // 模拟返回出库记录数据
        List<Object> records = new ArrayList<>(generateMockOutboundRecords(takingId));
        return UR.ok("出库记录查询成功", records);
    }
    
    /**
     * 查询操作记录
     */
    @GetMapping("/operation/{takingId}")
    public UR operationRecords(@PathVariable("takingId") Long takingId) {
        // 模拟返回操作记录数据
        List<Object> records = new ArrayList<>(generateMockOperationRecords(takingId));
        return UR.ok("操作记录查询成功", records);
    }
    
    /**
     * 将结果转换为UR
     */
    private UR toUR(int rows) {
        return rows > 0 ? UR.ok("操作成功") : UR.fail("操作失败");
    }
    
    /**
     * 生成模拟入库记录数据
     */
    private List<Map<String, Object>> generateMockInboundRecords(Long takingId) {
        // 这里可以根据需要生成模拟数据
        List<Map<String, Object>> records = new ArrayList<>();
        
        Map<String, Object> record1 = new HashMap<>();
        record1.put("inboundCode", "RK202401001");
        record1.put("materialCode", "M001");
        record1.put("materialName", "钢板");
        record1.put("specification", "2mm");
        record1.put("quantity", 50);
        record1.put("createTime", "2024-01-05 09:30:00");
        record1.put("createBy", "admin");
        records.add(record1);
        
        Map<String, Object> record2 = new HashMap<>();
        record2.put("inboundCode", "RK202401002");
        record2.put("materialCode", "M002");
        record2.put("materialName", "螺丝");
        record2.put("specification", "M8");
        record2.put("quantity", 2000);
        record2.put("createTime", "2024-01-06 14:20:00");
        record2.put("createBy", "admin");
        records.add(record2);
        
        return records;
    }
    
    /**
     * 生成模拟出库记录数据
     */
    private List<Map<String, Object>> generateMockOutboundRecords(Long takingId) {
        // 这里可以根据需要生成模拟数据
        List<Map<String, Object>> records = new ArrayList<>();
        
        Map<String, Object> record1 = new HashMap<>();
        record1.put("outboundCode", "CK202401001");
        record1.put("materialCode", "M001");
        record1.put("materialName", "钢板");
        record1.put("specification", "2mm");
        record1.put("quantity", 10);
        record1.put("createTime", "2024-01-08 11:15:00");
        record1.put("createBy", "admin");
        records.add(record1);
        
        Map<String, Object> record2 = new HashMap<>();
        record2.put("outboundCode", "CK202401002");
        record2.put("materialCode", "M002");
        record2.put("materialName", "螺丝");
        record2.put("specification", "M8");
        record2.put("quantity", 500);
        record2.put("createTime", "2024-01-09 16:40:00");
        record2.put("createBy", "admin");
        records.add(record2);
        
        return records;
    }
    
    /**
     * 生成模拟操作记录数据
     */
    private List<Map<String, Object>> generateMockOperationRecords(Long takingId) {
        // 这里可以根据需要生成模拟数据
        List<Map<String, Object>> records = new ArrayList<>();
        
        Map<String, Object> record1 = new HashMap<>();
        record1.put("operateType", "create");
        record1.put("operateBy", "admin");
        record1.put("operateContent", "创建了盘点单");
        record1.put("operateTime", "2024-01-10 10:30:00");
        record1.put("remark", "月度例行盘点");
        records.add(record1);
        
        Map<String, Object> record2 = new HashMap<>();
        record2.put("operateType", "submit");
        record2.put("operateBy", "admin");
        record2.put("operateContent", "提交了盘点单");
        record2.put("operateTime", "2024-01-10 15:45:00");
        record2.put("remark", "");
        records.add(record2);
        
        Map<String, Object> record3 = new HashMap<>();
        record3.put("operateType", "approve");
        record3.put("operateBy", "manager");
        record3.put("operateContent", "审核通过了盘点单");
        record3.put("operateTime", "2024-01-11 09:20:00");
        record3.put("remark", "数据无误，予以通过");
        records.add(record3);
        
        return records;
    }
} 